package com.tiny.mirror.service.product.support.strategy.lhc;

import com.google.common.collect.Lists;
import com.tiny.mirror.basic.common.code.CommonStatusCode;
import com.tiny.mirror.basic.web.exception.BusinessException;
import com.tiny.mirror.service.product.common.util.AbilityLhcPlayUtil;
import com.tiny.mirror.service.product.domain.ability.LhcParams.LhcItemParams;
import com.tiny.mirror.service.product.domain.ability.LhcParams.LhcPlayParams;
import com.tiny.mirror.service.product.enums.AbilityLhcCategoryEnum;
import com.tiny.mirror.service.product.enums.AbilityLhcStrategyEnum;
import com.tiny.mirror.service.product.support.strategy.lhc.dto.LhcNumberDTO;
import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;
import org.springframework.stereotype.Component;

@Component
public class LhcTmapStrategy extends AbstractLhcStrategy {

  @Override
  public AbilityLhcStrategyEnum strategy() {
    return AbilityLhcStrategyEnum.TMAP;
  }

  @Override
  public LhcPlayParams build() {
    LhcPlayParams params = new LhcPlayParams();
    params.setCode("tm-tmap");
    params.setRule(
        "选1个或1个以上号码，若开奖号码的特别码与所选号码一致，即为中奖。"
            + "\n例如：投注方案为45\n开奖号码为：04,25,36,22,44,30 + 45，即中特别码A盘"
    );
    params.setCategory(AbilityLhcCategoryEnum.TM);
    params.setPlay(AbilityLhcStrategyEnum.TMAP.getName());
    params.setStrategy(AbilityLhcStrategyEnum.TMAP);
    params.setLimit(1);
    List<LhcItemParams> itemParamsList = Lists.newArrayList();
    AbilityLhcPlayUtil.NUMBER.stream().forEach(i -> {
      LhcItemParams itemParams = new LhcItemParams();
      itemParams.setItem(i);
      itemParams.setOdds(new BigDecimal("49.99"));
      itemParamsList.add(itemParams);
    });
    params.setItemList(itemParamsList);
    return params;
  }

  @Override
  public BigDecimal calculate(
      LhcPlayParams lhcPlayParams, BigDecimal amount, LhcNumberDTO result, String... item
  ) {
    checkAmount(amount);
    checkItem(1, item[0]);
    return match(lhcPlayParams, amount, result, item[0]);
  }


  @Override
  public void checkItem(Integer limit, String... item) {
    if (item.length != limit) {
      throw new BusinessException(CommonStatusCode.COMMON_ERROR);
    }
    if (Arrays.stream(item).anyMatch(i -> !AbilityLhcPlayUtil.NUMBER.contains(i))) {
      throw new BusinessException(CommonStatusCode.COMMON_ERROR);
    }
  }
}
